capture log close
clear
set more off
timer clear 1
timer on 1
capture program drop _all
set seed 123 

**************** 
***  PATHS   ***  
****************
*BCCR: 1 if at Central Bank
global BCCR=1
if $BCCR == 1 {
	global path "Y:/Projects/MNC_Suppliers/Suppliers_Alfaro-Urena_Manelici_Vasquez"
}

if $BCCR == 0 {
	global path "~/Documents/GitHub/Suppliers_Alfaro-Urena_Manelici_Vasquez"
}

if "$last_year_revec"==""{
display "need to specify global last_year_revec"
stop_stop
}

cd $path 

*log file
local logfile "${path0}/log_files/3-main_results"
set linesize 250
log using `logfile', replace
set linesize 250

/*
MAIN REGRESSION ANALYSIS
*/

********************** 
***  INPUT FILES   ***  
**********************
global analysis_data ""processed_data/analysis_data.dta""
global revec_group ""raw_data/revec_group.dta""
global transactions ""raw_data/trans_clean_corresp.dta""
*dummies to show in tables and graphs 	
global dummies "D_m4 D_m3 D_m2 D_0 D_1 D_2 D_3 D_4"

********************************************************************************
* main program: outline of the full code
********************************************************************************
prog main

*Figure 1, Table 1
*Domestic Firms Increase Their Scale and TFP after Starting to Supply to MNCs
	firm_scale_tfp

*Figure 2, Table 2
*Domestic Firms Improve Their Sales to Others after Starting to Supply to MNCs
	firm_business_with_others

*Figure 3, Tables E2-E7, and Figures E1 and E2 
*Placebo demand shocks
	placebos

*Table 3 is produced in code Z-matching.do together with Table D1
	do ${path0}/all_codes/Z-matching.do
	
*Table 4
*First-time Suppliers Start Selling to More Buying Sectors. 
*Their Sales Shift Towards Buyers with Better Performance
	characteristics_buyers

*Table 5 and Table E9
*Model: First-Time Suppliers Have a Steep (Closer to Flat)
*Short-Run (Medium-Run) Marginal Cost Curve 
	model 
	
*Table 6
*Heterogeneity in TFP Gains Based on the Sector of the Domestic Firm or the MNC
	het_sector
	
*Table 7 
* Heterogeneity in TFP Gains Based on the Importance of the First Transaction
	tfp_dom_char_hete	
	
end

********************************************************************************
*Figure 1, Table 1
*Domestic Firms Increase Their Scale and TFP after Starting to Supply to MNCs
********************************************************************************
prog firm_scale_tfp

display "figure 1 and table 1.  firms increase their scale and productivity: Full Sample"
quiet{
set seed 123
*loading data	
use $analysis_data , clear
estimates clear

*DL 2013 
/*
*adaptation of De Loecker's code
*De Loecker, J. 2013. Detecting Learning by Exporting, American Economic Journal: Microeconomics.
*-Code last saved on December 10, 2012
COMMENT: SOMETIMES THE CODE DOES NOT RUN. IT PRODUCES 
 ERROR r(3000) "variable undeclared" or error r(3499) "MODEL1() not found"
 However, it is a matter of closing stata and opening it again for it to
 run properly
 */
do ${path0}/all_codes/Z-DL2013.do

* COLUMN 1: NEVER MATCHED + EVENTUALLY MATCHED, VAR: sales
global REG "reghdfe y  D_* if past!=1 , absorb(year#sector4#province IDs) vce(cluster province#sector_g)"
run_reg
estadd local Never_Matched "YES"
addfe Never_Matched
estimates store m_1 
***
*** GRAPH
***
quiet{
global outcome="Total_Sales"
global ranges="-0.1(0.1)0.4"
event_graph
local outcome="$outcome"
graph export "${path0}/results/0-main_text/Figure_1a.eps", as(eps) replace
}

* COLUMN 2: NEVER MATCHED + EVENTUALLY MATCHED, VAR: employment
global REG "reghdfe l  D_* if past!=1 , absorb(year#sector4#province IDs) vce(cluster province#sector_g)"
global quant "1"
run_reg 
estadd local Never_Matched "YES"
addfe Never_Matched
estimates store m_2
***
*** GRAPH
***
quiet{
global outcome="Number_of_Workers"
event_graph
local outcome="$outcome"
graph export "${path0}/results/0-main_text/Figure_1b.eps", as(eps) replace
}

* COLUMN 3: NEVER MATCHED + EVENTUALLY MATCHED, VAR: k
global REG "reghdfe k  D_* if past!=1 , absorb(year#sector4#province IDs) vce(cluster province#sector_g)" 
run_reg 
estadd local Never_Matched "YES"
addfe Never_Matched
estimates store m_3
***
*** GRAPH
***
quiet{
global outcome="Net_Assets"
event_graph
local outcome="$outcome"
graph export "${path0}/results/0-main_text/Figure_1c.eps", as(eps) replace
}

* COLUMN 4: NEVER MATCHED + EVENTUALLY MATCHED, VAR: m
global REG "reghdfe m  D_*  if past!=1, absorb(year#sector4#province IDs) vce(cluster province#sector_g)"
run_reg
estadd local Never_Matched "YES"
addfe Never_Matched
estimates store m_4
***
*** GRAPH
***
quiet{
global outcome="Input_Costs"
event_graph
local outcome="$outcome"
graph export "${path0}/results/0-main_text/Figure_1d.eps", as(eps) replace
}	
 
* COLUMN 5: NEVER MATCHED + EVENTUALLY MATCHED, VAR: CD FUNCTION
capture drop resid
global REG "reghdfe y k l m  D_* if past!=1 , absorb(year#sector4#province IDs) vce(cluster province#sector2)"
run_reg
estadd local Never_Matched "YES"
addfe Never_Matched
estimates store m_5
***
*** GRAPH
***
quiet{
global outcome="TFP_(CD_OLS)"
global ranges="-0.05(0.05)0.15"
event_graph
local outcome="$outcome"
graph export "${path0}/results/0-main_text/Figure_1e.eps", as(eps) replace
}
* COLUMN 6: NEVER MATCHED + EVENTUALLY MATCHED, VAR: TL FUNCTION
capture drop resid
global REG "reghdfe y k l m c.l#c.l c.k#c.k c.m#c.m c.k#c.l c.l#c.m c.k#c.m  D_*  if past!=1, absorb(year#sector4#province IDs) vce(cluster province#sector2)"
run_reg
estadd local Never_Matched "YES"
addfe Never_Matched
estimates store m_6

* COLUMN 7: NEVER MATCHED + EVENTUALLY MATCHED: REG Levinsohn and Petrin method
quiet{
prodest y if past!=1, free(l) state(k) proxy(m) met(lp) opt(dfp) reps(100) id(IDs) t(year)  fsresidual(res_LP) 
estadd scalar n_firms 
estadd scalar n_obs
estadd scalar n_fe
estadd local Never_Matched "YES"
addfe Never_Matched
estimates store p_3
capture drop resid
global REG "reghdfe res_LP  D_* if past!=1 , absorb(year#sector4#province IDs) vce(cluster province#sector2)"
$REG resid(resid)  
quietly summ res_LP if resid!=.
scalar mean_dep=r(mean)
scalar sd_dep=r(sd)
estadd scalar mean_dep
estadd scalar sd_dep
estadd scalar n_firms 
estadd scalar n_obs
estadd scalar n_fe
estadd local Never_Matched "YES"
addfe Never_Matched
estimates store m_7
}
display "$S_TIME  $S_DATE"
* COLUMN 8: NEVER MATCHED + EVENTUALLY MATCHED: REG ACF
quiet{
capture drop res_ACF
prodest y if past!=1, free(l ) state(k) proxy(m) met(lp) acf opt(dfp) reps(100) id(IDs) t(year) fsresidual(res_ACF)
estadd scalar n_firms 
estadd scalar n_obs
estadd scalar n_fe
estadd local Never_Matched "YES"
addfe Never_Matched
estimates store p_4

capture drop resid
global REG "reghdfe res_ACF  D_* if past!=1, absorb(year#sector4#province IDs) vce(cluster province#sector2)"
$REG resid(resid)  
***
*** GRAPH 
***
quiet{
global outcome="TFP_(ACF_2015)"
global ranges="-0.05(0.05)0.1"
event_graph
local outcome="$outcome"
graph export "${path0}/results/0-main_text/Figure_1f.eps", as(eps) replace
}
quietly summ res_ACF if resid!=.
scalar mean_dep=r(mean)
scalar sd_dep=r(sd)
estadd scalar mean_dep
estadd scalar sd_dep
estadd scalar n_firms 
estadd scalar n_obs
estadd scalar n_fe
estadd local Never_Matched "YES"
addfe Never_Matched
estimates store m_8
}
display "$S_TIME  $S_DATE"
* COLUMN 9: NEVER MATCHED + EVENTUALLY MATCHED: REG DL 2013
quiet{
capture drop resid
global REG "reghdfe prod_DL  D_* if past!=1, absorb(year#sector4#province IDs) vce(cluster province#sector2)"
$REG resid(resid)  
quietly summ prod_DL if resid!=.
scalar mean_dep=r(mean)
scalar sd_dep=r(sd)
estadd scalar mean_dep
estadd scalar sd_dep
estadd scalar n_firms 
estadd scalar n_obs
estadd scalar n_fe
estadd local Never_Matched "YES"
addfe Never_Matched
estimates store m_9

}
* Prepare estimates for -estout-
	estfe m_*, labels(IDs "Firm FE" year#sector4#province "Year-4DSect-Prov FE")
	return list	
}	
esttab m_* using ${path0}/results/0-main_text/Table_1.tex , indicate(`r(indicate_fe)' Never_Matched) ///
 star(* 0.10 ** 0.05 *** 0.01) varwidth(23) keep(${dummies}) cells("b(fmt(3) star )" "se(par fmt(3))" ) ///
	stats(r2_a mean_dep sd_dep n_obs n_fe n_firms , fmt(a2) labels("Adjusted R$^2$" "Mean Dep. Var. (level)" "SD Dep. Var. (level)" "\# Observations" "\# Fixed Effects" "\# Firms" ))   ///
	mlabel("Total Sales" "Number Workers" "Net Assets" "Input Costs" "CD OLS" "TL OLS" "LP (2003)" "ACF (2015)" "DL* (2013)") alignment(c) ///
    replace noconstant label nodepvar collabels(none)   compress	type  
end

********************************************************************************
*Figure 2, Table 2
*Domestic Firms Improve Their Sales to Others after Starting to Supply to MNCs
********************************************************************************
prog firm_business_with_others

display "Figure 2, Table 2. Sales to others. Full Sample"
quiet{
set seed 123
*loading data	
use $analysis_data , clear

estimates clear
* COLUMN 1: NEVER MATCHED + EVENTUALLY MATCHED, VAR: sales
global REG "reghdfe y  D_* if past!=1 , absorb(year#sector4#province IDs) vce(cluster province#sector_g)"
run_reg
estadd local Never_Matched "YES"
addfe Never_Matched
estimates store m_1
***
*** GRAPH
***
quiet{
global outcome="Total_Sales"
global ranges="-0.1(0.1)0.4"
event_graph
local outcome="$outcome"
graph export "${path0}/results/0-main_text/Figure_2a.eps", as(eps) replace
}
* sales to clients other than the MNCs triggering the event
* COLUMN 2: NEVER MATCHED + EVENTUALLY MATCHED, VAR: Sales to to others
global REG "reghdfe l_not_MNC_event D_* if past!=1 , absorb(year#sector4#province IDs) vce(cluster province#sector_g)"
run_reg
estadd local Never_Matched "YES"
addfe Never_Matched
estimates store m_2
***
*** GRAPH
***
quiet{
global outcome="Sales_to_Others"
global ranges=""
event_graph
local outcome="$outcome"
graph export "${path0}/results/0-main_text/Figure_2b.eps", as(eps) replace
}

* Excluding buyers experiencing an event
* COLUMN 3: NEVER MATCHED + EVENTUALLY MATCHED, VAR: Sales to to others (minus evented)
global REG "reghdfe l_not_evented D_* if past!=1 , absorb(year#sector4#province IDs) vce(cluster province#sector_g)"
run_reg
estadd local Never_Matched "YES"
addfe Never_Matched
estimates store m_3

* Excluding buyers who have been suppliers to MNCs
* COLUMN 4: NEVER MATCHED + EVENTUALLY MATCHED, VAR: Sales to to others (minus to suppliers)
global REG "reghdfe l_not_supp D_* if past!=1 , absorb(year#sector4#province IDs) vce(cluster province#sector_g)"
run_reg
estadd local Never_Matched "YES"
addfe Never_Matched
estimates store m_4

* COLUMN 5: NEVER MATCHED + EVENTUALLY MATCHED, VAR: transactions
global REG "reghdfe l_trans  D_* if past!=1 , absorb(year#sector4#province IDs) vce(cluster province#sector_g)"
run_reg
estadd local Never_Matched "YES"
addfe Never_Matched
estimates store m_5
***
*** GRAPH
***
quiet{
global outcome="Total_Corp._Sales"
event_graph
local outcome="$outcome"
local outcome: subinstr local outcome "." "" 
graph export "${path0}/results/0-main_text/Figure_2c.eps", as(eps) replace
}

* sales to clients other than the MNCs triggering the event
* COLUMN 6: NEVER MATCHED + EVENTUALLY MATCHED, VAR: Sales to to others
global REG "reghdfe l_not_MNC_event_2 D_* if past!=1 , absorb(year#sector4#province IDs) vce(cluster province#sector_g)"
run_reg
estadd local Never_Matched "YES"
addfe Never_Matched
estimates store m_6
***
*** GRAPH
***
quiet{
global outcome="Corp._Sales_to_Others"
event_graph
local outcome="$outcome"
local outcome: subinstr local outcome "." "" 
graph export "${path0}/results/0-main_text/Figure_2d.eps", as(eps) replace
}

* COLUMN 7: NEVER MATCHED + EVENTUALLY MATCHED, VAR: NUMBER OF CLIENTS
reghdfe l_not_MNC_event_2 D_* if past!=1 , absorb(year#sector4#province IDs) ///
 vce(cluster province#sector_g) resid(res1)
global REG "reghdfe l_clients_non_event D_* if past!=1 & res1!=., absorb(year#sector4#province IDs) vce(cluster province#sector_g)"
global quant "1"
run_reg 
estadd local Never_Matched "YES"
addfe Never_Matched
estimates store m_7
***
*** GRAPH
***
quiet{
global outcome="Number_of_Other_Corp._Buyers"
event_graph
local outcome="$outcome"
local outcome: subinstr local outcome "." "" 
graph export "${path0}/results/0-main_text/Figure_2e.eps", as(eps) replace
}

* COLUMN 8: NEVER MATCHED + EVENTUALLY MATCHED, VAR: AVERAGE SALES PER CLIENT (NOT TRIGGERING EVENT)
capture drop resid*
global REG "reghdfe l_ave2 D_* if past!=1 & res1!=. , absorb(year#sector4#province IDs) vce(cluster province#sector_g)"
run_reg
estadd local Never_Matched "YES"
addfe Never_Matched
estimates store m_8
***
*** GRAPH
***
quiet{
global outcome="Average_Corp._Sales_to_Others"
event_graph
local outcome="$outcome"
local outcome: subinstr local outcome "." "" 
graph export "${path0}/results/0-main_text/Figure_2f.eps", as(eps) replace
}

* Prepare estimates for -estout-
	estfe m_*, labels(IDs "Firm FE" year#sector4#province "Year-4DSect-Prov FE")
	return list	
}
esttab m_* using ${path0}/results/0-main_text/Table_2.tex, ///
    indicate(`r(indicate_fe)' Never_Matched)  star(* 0.10 ** 0.05 *** 0.01) varwidth(23) keep(${dummies}) cells("b(fmt(3) star )" "se(par fmt(3))" ) ///
	stats(r2_a mean_dep sd_dep n_obs n_fe n_firms , fmt(a2) labels("Adjusted R$^2$" "Mean Dep. Var. (level)" "SD Dep. Var. (level)" "\# Observations" "\# Fixed Effects" "\# Firms" ))   ///
	mlabel("Total Sales" "Sales to Others" "Sales to Others Robust 1" "Sales to Others Robust 2" "Total Corp. Sales" "Corp. Sales to Others" "Number Other Corp. Buyers" "Average Corp. Sales to Others") alignment(c) ///
    replace noconstant label nodepvar collabels(none)   compress	type
end

 
********************************************************************************
*Figure 3, Tables E2-27, and Figures E1 and E2 
*Placebo demand shocks
********************************************************************************
{
prog placebos

*GOV exercise
global type_ "gov"
global label_name "Government"
	quiet sample_${type_}
	placebo_routine
	
*BIG DOMESTIC FIRMS exercise
global type_ "bigdom"	
global label_name "Large Domestic Buyers"
	quiet sample_${type_}
	placebo_routine

*Domestic exporters exercise
global type_ "exp"	
global label_name "Domestic Exporters"
	quiet sample_${type_}
	placebo_routine
end

*********************************
*Placebo exercises: common codes
*********************************
prog placebo_routine
set seed 123  
global match ""
global matched_label ""
* DATA 0: EVENT WITH MNCs
global analysis_data0 $analysis_data

*first time suppliers 
	quiet suppliers
		
*descriptive stats code + matching
	desc_and_matching
	
end 

*********************
*selecting gov firms
*********************
prog sample_gov
display "selecting government firms"

quiet{
set seed 123 
* EXCLUDING FIRMS THAT EXPERIENCED AN EVENT WITH MNC
use $analysis_data, clear
drop if event!=.
keep event past ID year ingresos
rename * seller_*
rename seller_ID seller
rename seller_year year
tempfile events
save `events', replace

*IMPORTING ALL FIRMS
use $revec_group, clear
drop if inolucrogrupo==1
*BUYERS: GOVERNMENT 
keep if ipublicas==1
* not financial or education
drop if sector_g==13 
drop if sector_g==17 
gen notpurch= strpos(nombre_, "municipal") > 0
replace notpurch= 1 if strpos(nombre_, "cantonal") > 0
drop if notpurch==1

keep ID nombre_ sector_g sector_i 
rename * b_*
rename b_ID buyer
tempfile as_buyer
save `as_buyer', replace

* TRANSACTIONS
use $transactions, clear
drop if pot==1
quiet{
* TRANSACTIONS TO REAL VALUE
local trans_vari "trans"
foreach v of local trans_vari{
forvalue i=2008/ $last_year_revec {
replace `v'=(100*(`v')/${ipc`i'})/1000000 if year==`i'
}
}
}
drop pot

** MERGING FIRMS
merge m:1 buyer using `as_buyer'
gen buyer_gov=(_m==3)
drop if _m==2
drop _m
merge m:1 seller year using `events'
keep if _m==3
drop _m

}
*saving temp data
save temp/temp_gov.dta , replace
end 

*************************
*selecting big dom firms
*************************
prog sample_bigdom
display "selecting Big Dom firms"

quiet{
set seed 123 
* EXCLUDING FIRMS THAT EXPERIENCED AN EVENT WITH MNC
use $analysis_data, clear
drop if event!=.
keep event past ID year ingresos
rename * seller_*
rename seller_ID seller
rename seller_year year
tempfile events
save `events', replace

*IMPORTING ALL FIRMS
use $analysis_data, clear
bys ID: egen med_workers=median(trabaj_)
keep if med_w>100
distinct ID
bys ID: gen dup=cond(_N==1,0,_n)
drop if dup>1
keep ID   
preserve 
rename ID seller
tempfile as_seller
save `as_seller', replace
restore
rename * b_*
rename b_ID buyer
tempfile as_buyer
save `as_buyer', replace

* TRANSACTIONS
use $transactions, clear
drop if pot==1
quiet{
* TRANSACTIONS TO REAL VALUE
local trans_vari "trans"
foreach v of local trans_vari{
forvalue i=2008/ $last_year_revec {
replace `v'=(100*(`v')/${ipc`i'})/1000000 if year==`i'
}
}
}
drop pot

** MERGING FIRMS
merge m:1 buyer using `as_buyer'
gen buyer_bigdom=(_m==3)
drop if _m==2
drop _m
merge m:1 seller year using `events'
keep if _m==3
drop _m
*excluding the large buyers when they are sellers
merge m:1 seller using `as_seller'
drop if _m==3
drop _m

}
*saving temp data
save temp/temp_bigdom.dta , replace
end 

*************************
*selecting exp dom firms
*************************
prog sample_exp
display "selecting exporting firms"

quiet{
set seed 123 
* EXCLUDING FIRMS THAT EXPERIENCED AN EVENT WITH MNC
use $analysis_data, clear
drop if event!=.
keep event past ID year ingresos
rename * seller_*
rename seller_ID seller
rename seller_year year
tempfile events
save `events', replace

*IMPORTING ALL FIRMS
use $analysis_data, clear
gen temp_exporter=(exports>0 & exports!=.)
bys ID: egen temp_years_exporter=total(temp_exporter)
bys ID: gen temp_years=_N
*exporters all years
gen exporter_=(temp_years_exporter==temp_years)
keep if exporter_==1

bys ID: gen dup=cond(_N==1,0,_n)
drop if dup>1
keep ID   
preserve 
rename ID seller
tempfile as_seller
save `as_seller', replace
restore
rename * b_*
rename b_ID buyer
tempfile as_buyer
save `as_buyer', replace

* TRANSACTIONS
use $transactions, clear
drop if pot==1
quiet{
* TRANSACTIONS TO REAL VALUE
local trans_vari "trans"
foreach v of local trans_vari{
forvalue i=2008/ $last_year_revec {
replace `v'=(100*(`v')/${ipc`i'})/1000000 if year==`i'
}
}
}
drop pot

** MERGING FIRMS
merge m:1 buyer using `as_buyer'
gen buyer_exp=(_m==3)
drop if _m==2
drop _m
merge m:1 seller year using `events'
keep if _m==3
drop _m
*excluding the buyers when they are sellers
merge m:1 seller using `as_seller'
drop if _m==3
drop _m

}
*saving temp data
save temp/temp_exp.dta , replace
end 

***************************************
*first time suppliers in each exercise
***************************************
prog suppliers
display "first time suppliers in the ${type_} exercise"

quiet{
set seed 123 
*firm level info
use $analysis_data , clear
keep ID year ingresosir total_activo_neto_ costo_de_ salario ///
 trabaj_ sector4 provincia n_clients all_trans prod sector2 sector_g
tempfile firm_info
save `firm_info', replace

*using previous data 
use temp/temp_${type_}.dta , clear

gen year1=buyer_${type_}*year
replace year1=. if year1==0
bys seller: egen event_${type_}=min(year1)
gen past_${type_}=1 if event==2008 | event==2009
replace event_${type_}=. if event_${type_}==2008 | event_${type_}==2009 | event_${type_}==0
drop year1
* # Gov clients and trans
bys seller year: egen n_${type_}=total(buyer_${type_})
gen trans2=buyer_${type_}*trans
bys seller year: egen ${type_}_trans=total(trans2)

*length, buyer, and magnitude of the event 
preserve
keep if buyer_${type_}==1 & event!=.
gsort seller year -trans_
by seller year: gen temp_triggering=(_n==1)
replace temp_triggering=0 if year!=event
bys seller buyer: egen triggering=max(temp_triggering)
keep if triggering==1
bys seller buyer: gen length=_N
keep if year==event
rename ${type_}_trans first_trans
keep seller length first_trans buyer
tempfile length
save `length', replace
restore

*buyers 
preserve 
keep if buyer_${type_}==1
keep if event==year
gen temp_trans=-trans_
sort seller year temp_trans, stable 
by seller: gen dup=cond(_N==1,0,_n)
drop if dup>1
keep seller buyer 
save temp/buyers_type_, replace
restore 

collapse (sum) trans_ (mean) ///
 ${type_}_trans n_${type_} event_${type_} past_${type_} seller_ingre, by(seller year)
merge m:1 seller using `length'
drop _m
merge m:1 seller using temp/buyers_type_
drop _m
 
gen sh_=${type_}_trans/trans_
replace ${type_}_trans=seller_ingre*sh_ if trans_>seller_ingre & trans_!=.
drop sh_ trans_
rename seller ID
*merging firm level info
merge 1:1 ID year using `firm_info'
replace ${type_}_trans=0 if _m==2
replace n_${type_}=0 if _m==2  
drop _m 
local year_minus2= $last_year_revec - 2 
drop if event_${type_}>`year_minus2' & event_${type_}!=.
** creating important variables
* domestic clients
gen l_trans=log(all_trans)
gen y=log(ingresos)
* sales to clients other than the Gov 
gen log_trans_not_${type_}=log(all_trans-${type_}_trans)
gen not${type_}_clients=n_clients-n_${type_} +1 if log_trans_not_${type_}!=.
gen l_clients_non_event=log(not${type_}_clients)
gen log_sales_not_${type_}= log(all_trans- ${type_}_trans)
gen l_sales_per_client=log_sales_not_${type_}-log(not${type_}_clients)
*l_ave2
gen l_trans_per_client=log_trans_not_${type_}-log(not${type_}_clients)


egen province=group(provincia)
egen IDs=group(ID)

*DUMMIES FOR EVENT STUDY
gen l=log(trabaj)
gen k=log(total_activo_neto_)
gen m=log(costo_de)
quiet{
gen time=year-event_${type_}
local lower_lim=-5
local upper_lim=5

scalar first_year=`lower_lim'
scalar last_year=`upper_lim'
local dummies ""

forvalue i=`=first_year'(1)`=last_year'{
if `i'<0{
scalar j=abs(`i')
gen D_m`=j' =( time==`i')
label var D_m`=j' "t $ =`i' $"
if `i'==`=first_year'{
replace D_m`=j' =( time<=`i' & time!=.)
label var D_m`=j' "t $ \leq `i'$"
}
*if `i'!=`=first_year' & `i'!=-1{
if `i'!=-1{
local dummies="`dummies' D_m`=j'"
}
}
if `i'>=0{
gen D_`i' = (time==`i')
label var D_`i' "t $ =`i'$"
if `i'==`=last_year'{
replace D_`i' = (time>=`i'  & time!=.)
label var D_`i' "t $ \geq `i'$"
}    
*if `i'!=`=last_year'{
local dummies="`dummies'  D_`i'"
*}
}
}
drop D_m1
gen time_reg=time
replace time_reg=`upper_lim' if time>=`upper_lim' & time!=.
replace time_reg=`lower_lim' if time<=`lower_lim'
}
}
gen treated=(event_${type_}!=.)
*saving temp data
capture rename log_sales_not_${type_} l_not_event
capture rename log_trans_not_${type_} l_not_event_trans 
save temp/temp_${type_}2.dta , replace
* DATA 1: EVENT WITH other firms 
global analysis_data_${type_} temp/temp_${type_}2.dta
*regression	
	global tab_name "${type_}"

end

*****************************************
*placebos: descriptive statistics code
*****************************************
prog desc_and_matching
display "*table ${type_} vs mnc descriptives"

* DATA 1: EVENT WITH other firms 
global analysis_data1 ""${analysis_data_${type_}}""

************
*the suppliers 
************
display "*table ${type_} vs mnc descriptives: suppliers"
quiet{
set seed 123 
*table's number 
if "${tab_name}"=="gov"{
global tab_n "E5"
global typename "Govern."
}
if "${tab_name}"=="bigdom"{
global tab_n "E6"
global typename "Large Dom"
}
if "${tab_name}"=="exp"{
global tab_n "E7"
global typename "Dom Expor"
}
*import data
use $analysis_data0, clear
*length for MNC events
preserve
keep ID year
rename ID seller
tempfile seller
save `seller', replace
use $transactions, clear
drop if pot==1
merge m:1 seller year using `seller'
keep if _m==3
drop _m
bys buyer seller: gen length= _N
collapse length , by(buyer seller)
rename seller ID
tempfile length
save `length', replace
restore
*merging length
preserve
collapse year, by(ID buyer)
drop year
merge 1:1 ID buyer using `length'
drop if _m==2
drop _m
tempfile length
save `length', replace
restore
merge m:1 ID buyer using `length'
drop _m 
rename trans_first_mnc first_trans
gen type=0
append using $analysis_data1
replace type=1 if type==.

*to th dollars 
foreach v in ingresos first_trans {
*approx exchange rate=500
replace `v'=`v'*1000/500
*}
}

*share of first trans
gen share_first_trans=first_trans/ingresosir

*keeping only the observations included in the regressions
reghdfe y  D_*  if type==0 & past_t!=1 , absorb(year#sector4#province IDs) vce(cluster province#sector_g) resid(temp_res_mnc)
reghdfe y  D_*  if type==1 & past_${type_}!=1 , absorb(year#sector4#province IDs) vce(cluster province#sector_g) resid(temp_res_${type_})
bys ID type: egen res_mnc=min(temp_res_mnc)
bys ID type: egen res_${type_}=min(temp_res_${type_})
keep if res_mnc!=. | res_${type_}!=.
keep if event!=. | event_${type_}!=.

keep if time==0

*sales per worker 
gen y_per_l = ingresos/trabaj_

*firms in San Jose
gen san_jose=(provincia=="san jose")

*broad sector
tab sector_g, gen(sects)
global variab "ingresos trabaj_ y_per_l n_clients length first_trans share_first_trans"
save temp/descriptives_${type_}.dta , replace

eststo clear 
myttests sects* san_jose $variab , by(type) 
distinct ID if type==0
scalar events_mnc=r(ndistinct)
distinct ID if type==1
scalar events_${type_}=r(ndistinct)
estadd scalar events_mnc
estadd scalar events_${type_} 
*distinct buyers
distinct buyer if type==0 
scalar buyers_mnc=r(ndistinct)
distinct buyer if type==1 
scalar buyers_${type_}=r(ndistinct)
estadd scalar buyers_mnc
estadd scalar buyers_${type_}

}
display "column 1: MNC events, column 2: ${type_} events"
esttab using ${path0}/results/5-appendix_e\Supplementary_Table_${tab_n}a.tex ,  ///
    cells("mu_1(fmt(a3)) mu_2 d(star pvalue(d_p))" ///
	"mu_1_se(par fmt(2)) mu_2_se(par fmt(2)) d_se(par fmt(2))" ) ///
	collabels("First-time MNC Suppliers" "First-time ${typename} Suppliers" "Diff.") ///
	stats(buyers_mnc buyers_${type_} events_mnc  events_${type_} , fmt(0 0) ///
	labels("\# Buyers MNCs" "\# Buyers ${type_}" "\# Events MNCs" "\# Events ${type_}" )) label ///
	replace type note("column 1: MNC events, column 2: ${type_} events")
	
************
*matching MNC suppliers on descriptives
************
placebos_matching

************
*the buyers 
************
display "*table ${type_} vs mnc descriptives: buyers"
quiet{
set seed 123 
*table's number 
if "${tab_name}"=="gov"{
global tab_n "E2"
global typename "Gov"
}
if "${tab_name}"=="bigdom"{
global tab_n "E3"
global typename "Large Dom"
}
if "${tab_name}"=="exp"{
global tab_n "E4"
global typename "Dom Expor"
}
use temp/descriptives_${type_}.dta, clear
*unique buyers
bys type buyer: gen dup=cond(_N==1,0,_n)
drop if dup>1
keep buyer type 
rename buyer ID
tempfile buyers
save `buyers'
**
*data from revec
**
use $revec_group , clear
merge 1:1 ID using `buyers'
keep if _m==3
keep ID type trabaj_* ingresosir* provincia sector_g imports* 

* money in Th dollars 
forvalue year =2008/ $last_year_revec {
foreach vari in ingresosir{
replace `vari'_`year'= (100*abs(`vari'_`year')/${ipc`year'})/1000/500
format `vari'_`year' %16.0g
}
}
reshape long trabaj_ ingresosir_ imports_ , i(ID) j(year)
replace imports=imports/1000
drop if year<2008 | year> $last_year_revec 
if "${type_}"!="gov"{
*gov firms usually do not report sales 
drop if ingresos==. | trabaj==. 
} 
drop if (ingresos==. | trabaj==. )==1 & type==0
**
*data from transactions 
**
preserve
use $transactions , clear
drop if pot==1
*length
bys buyer seller: gen length=_N
bys buyer seller: gen dup=cond(_N==1,0,_n)
drop if dup>1
collapse length , by(buyer)
rename buyer ID
tempfile length
save `length', replace
restore 

preserve
use $transactions , clear
drop if pot==1
gen suppliers=1
*length
bys buyer: egen tot_trans=total(trans)
bys buyer seller: egen pair_trans=total(trans)
bys buyer seller: gen temp_lenght=_N

collapse (sum) suppliers trans , by(buyer year)
forvalue year =2008/ $last_year_revec {
replace trans= (100*trans/${ipc`year'})/1000/500 if year==`year'
}
rename buyer ID
tempfile buyers
save `buyers', replace
restore

merge 1:1 ID year using `buyers'
keep if _m==3
*averages across time
collapse ingresos trabaj trans suppliers imports , by(ID type sector_g provincia)
merge 1:1 ID using `length'
keep if _m==3

*sales per worker 
gen y_per_l = ingresos/trabaj_

*firms in San Jose
gen san_jose=(provincia=="san jose")

*share of imports
gen sh_imports=imports/(imports+trans)
replace sh_imports=0 if sh_imports==.

*broad sector
tab sector_g, gen(sects)
eststo clear
bys type: summ sects* san_jose ingresos trabaj_ y_per_l suppliers length sh_imports
myttests sects* san_jose ingresos trabaj_ y_per_l suppliers length sh_imports, by(type) 
distinct ID if type==0
scalar events_mnc=r(ndistinct)
distinct ID if type==1
scalar events_${type_}=r(ndistinct)
estadd scalar events_mnc
estadd scalar events_${type_}

}
display "column 1: MNC events, column 2: ${type_} events"
esttab using ${path0}/results/5-appendix_e\Supplementary_Table_${tab_n}.tex , nomtitle nonumbers  ///
    cells("mu_1(fmt(a3)) mu_2 d(star pvalue(d_p))" ///
	"mu_1_se(par fmt(2)) mu_2_se(par fmt(2)) d_se(par fmt(2))" ) ///
	collabels("First MNC Buyers" "First ${typename} Buyers" "Diff.") ///
	stats(events_mnc  events_${type_} , fmt(0 0) ///
	labels("\# Buyers MNCs" "\# Buyers ${type_}" )) label ///
	replace type note("column 1: MNC events, column 2: ${type_} events")
	
end

*******************************************
*matching to make the events more similar
*******************************************
program placebos_matching
****
*for each firm in the event with other firms we want to find the closest
*supplier to MNC
****
display "matching $type_ : finding the closest supplier to MNC"
quiet{
set seed 123 
use temp/descriptives_${type_}.dta , clear
**
**computing z-scores
**
capture drop z_*
foreach v in $variab {
summ `v'
scalar mean_ = `r(mean)'
scalar sd_ =`r(sd)'
gen z_`v'=(`v'-`=mean_')/`=sd_'
}
**
**finding the match
**
egen id_number=group(ID) if type==1
summ id_number 
local max_ `r(max)'
tempfile match
}
forvalue i=1/`max_' {
scalar j=mod(`i',250)
if `=j'==0{
display "match ${type_}: firm `i' of `max_'"
}
quiet{
preserve
*firm to match
gen matching= (id_number==`i' )

*no other treated in this iteration
drop if type==1 & matching==0

*keeping sector of the firm
summ sector_g if matching==1
keep if sector_g==`r(mean)'

*not in SJ
summ san_jose if matching==1
keep if san_jose==`r(mean)'

*check only one treated firm 
distinct ID if type==1
capass `r(ndistinct)'==1 , throw("taking more than one matched at a time")

*scores across variables
foreach v in $variab {
quiet gen temp1_`v'=z_`v' if matching==1
egen z_ID_`v'=mean(temp1)
gen score_`v'=(z_`v'- z_ID_`v')^2
capture drop temp*
}
egen score=rowtotal(score_*)
drop score_*
drop id_number
gen id_number=`i'
drop if matching==1
sort score 
keep if _n==1
keep ID id_number
 
if `i'>1{
*appending others
append using `match'
}

*saving 
save `match' , replace
restore 
}
}
quiet{
use `match', replace
save temp/match_${type_}.dta , replace
**
** firms matched
**
preserve
use temp/match_${type_}.dta, clear
tempfile ids
save `ids'
restore
use temp/descriptives_${type_}.dta , clear
joinby ID using `ids' , unmatched(both)
*merge m:1 ID using `ids'
keep if id_number!=. | type==1 
**
** descriptives 
**
eststo clear
myttests sects* san_jose $variab , by(type) 
egen g=group(ID id)
distinct g if type==0
scalar events_mnc=r(ndistinct)
distinct ID if type==1
scalar events_${type_}=r(ndistinct)
estadd scalar events_mnc
estadd scalar events_${type_}
*distinct buyers
distinct buyer if type==0 
scalar buyers_mnc=r(ndistinct)
distinct buyer if type==1 
scalar buyers_${type_}=r(ndistinct)
estadd scalar buyers_mnc
estadd scalar buyers_${type_}
}
display "column 1: MNC events, column 2: ${type_} events"
esttab using ${path0}/results/5-appendix_e\Supplementary_Table_${tab_n}b.tex , nomtitle nonumbers  ///
    cells("mu_1(fmt(a3)) mu_2 d(star pvalue(d_p))" ///
	"mu_1_se(par fmt(2)) mu_2_se(par fmt(2)) d_se(par fmt(2))" ) ///
	collabels("First-time MNC Suppliers" "First-time ${typename} Suppliers" "Diff.") ///
	stats(buyers_mnc buyers_${type_} events_mnc  events_${type_} , fmt(0 0) ///
	labels("\# Buyers MNCs" "\# Buyers ${type_}" "\# Events MNCs" "\# Events ${type_}" )) label ///
	replace type note("column 1: MNC events, column 2: ${type_} events")

***
*** regressions
***	
quiet{

use temp/match_${type_}.dta, clear
collapse id ,by(ID)
tempfile ids
save `ids'

use $analysis_data , clear
merge m:1 ID using `ids'
keep if _m==3 | event==.
drop _m

*saving 
save temp\temp_analysis_match_${type_}.dta , replace
global match "_match"
global analysis_data0 "temp\temp_analysis_match_${type_}.dta"
global matched_label "(matched)"
}

*figure vs mnc effect
	quiet fig_vs_mnc
	
end
	
************************
*figures vs mnc effect
************************
prog fig_vs_mnc
display "*figure vs mnc effect ${matched_label}"
quiet{

*outcomes 
global name_1 "Total_Sales"
global outcome_1 "y"
global range_1 "-0.2(0.2)0.4"
global loc_1 "5-appendix_e"
if "${tab_name}"=="gov"{
global fig_n_1 "Supplementary_Figure_E1a"
}
if "${tab_name}"=="bigdom"{
global fig_n_1 "Supplementary_Figure_E1c"
}
if "${tab_name}"=="exp"{
global fig_n_1 "Supplementary_Figure_E1e"
}

global name_2 "Number_of_Workers"
global outcome_2 "l"
global range_2 "-0.2(0.2)0.4"
global loc_2 "5-appendix_e"
if "${tab_name}"=="gov"{
global fig_n_2 "Supplementary_Figure_E1b"
}
if "${tab_name}"=="bigdom"{
global fig_n_2 "Supplementary_Figure_E1d"
}
if "${tab_name}"=="exp"{
global fig_n_2 "Supplementary_Figure_E1f"
}

global name_3 "TFP_(CD_OLS)"
global outcome_3 "y k l m"
global range_3 "-0.05(0.05)0.15"
global loc_3 "0-main_text"
if "${tab_name}"=="gov"{
global fig_n_3 "Figure_3a"
}
if "${tab_name}"=="bigdom"{
global fig_n_3 "Figure_3c"
}
if "${tab_name}"=="exp"{
global fig_n_3 "Figure_3e"
}

global name_4 "Corp._Sales_to_Others"
global outcome_4 "l_not_event_trans"
global range_4 "-1(0.5)0.75"
global loc_4 "0-main_text"
if "${tab_name}"=="gov"{
global fig_n_4 "Figure_3b"
}
if "${tab_name}"=="bigdom"{
global fig_n_4 "Figure_3d"
}
if "${tab_name}"=="exp"{
global fig_n_4 "Figure_3f"
}

global name_5 "Number_of_Other_Corp._Buyers"
global outcome_5 "l_clients_non_event"
global range_5 "-0.2(0.2)0.4"
global loc_5 "5-appendix_e"
if "${tab_name}"=="gov"{
global fig_n_5 "Supplementary_Figure_E2a"
}
if "${tab_name}"=="bigdom"{
global fig_n_5 "Supplementary_Figure_E2c"
}
if "${tab_name}"=="exp"{
global fig_n_5 "Supplementary_Figure_E2e"
}

global name_6 "Average_Corp._Sales_to_Others"
global outcome_6 "ave_trans"
global range_6 "-1(0.5)0.5"
global loc_6 "5-appendix_e"
if "${tab_name}"=="gov"{
global fig_n_6 "Supplementary_Figure_E2b"
}
if "${tab_name}"=="bigdom"{
global fig_n_6 "Supplementary_Figure_E2d"
}
if "${tab_name}"=="exp"{
global fig_n_6 "Supplementary_Figure_E2f"
}

global first_outcome=1
global last_outcome=6
global analysis_data1 ""${analysis_data_${type_}}""
 
estimates clear
*loop over outcomes 
forvalue outc= $first_outcome / $last_outcome {
global outcome_ ${outcome_`outc'}
forvalues r=0/1{

*using data 
use ${analysis_data`r'}, clear
ds D_m4-D_4
global dummies `r(varlist)'

*standard variable names 
capture rename l_not_MNC_event l_not_event
capture rename l_not_MNC_event_2 l_not_event_trans
capture rename l_trans_per_client ave_trans
capture rename l_ave2 ave_trans
capture replace l_clients_non_event=. if l_not_event_trans==.

capture drop red
global REG "reghdfe $outcome_  D_*  if past!=1 , absorb(year#sector4#province IDs) vce(cluster province#sector_g)"
*regression 
run_reg 
estimates store m_`r'_`outc'
*graph
parmby "$REG",label norestore
drop if label=="" | label=="Constant"
sencode label,gene(parmlab)

*only for symmetric intervals before/after
scalar upper=_N/2
scalar lower=-`=upper'
scalar lowerplus=`=lower'+1
scalar upperminus=`=upper'-1
local time "`=lower'"
forvalue i=`=lowerplus'/`=upper'{
if `i'!=-1{
local time "`time' `i'" 
}
}
egen time = fill(`time')
replace time=`=upper' if _N==_n
drop if time==`=upper' | time==`=lower'
local new = _N + 1
set obs `new' 
local new = _N 
replace estimate=0 in `new'
replace stderr=0 in `new'
replace time=-1 in `new'
label var time "Years Since Interaction"
label var estimate "`outcome'"
local labels "`=lowerplus' `"`=lowerplus'"'"
scalar lowerplus2=`=lowerplus'+1
display `labels'
forvalue i=`=lowerplus2'/`=upperminus'{
local labels "`labels' `i' `"`i'"'"
}
display `labels'
sort time
gen gov=`r'
save temp/info_`r'_`outc'.dta , replace
}
}

*regression by outcome 
forvalue outc= $first_outcome / $last_outcome {

local ranges "${range_`outc'}"
*using data 
use temp/info_0_`outc'.dta , clear
append using temp/info_1_`outc'.dta

*moving a bit the x-axis
replace time= time-0.05 if gov==1
replace time= time+0.05 if gov==0

*figure 
local outcome "${name_`outc'}"
local outcome_file: subinstr local outcome "." "" 
local outcome: subinstr local outcome "_" " " 
local outcome: subinstr local outcome "_" " " 
local outcome: subinstr local outcome "_" " " 
local outcome: subinstr local outcome "_" " " 
global outc_ `outcome'


twoway rcap max95 min95 time if gov==0 , /// 
lc(emerald) lpattern(solid) ///
||     scatter estimate time if gov==0, ///
mc(emerald) msymbol(O) ///
||     rcap  max95 min95 time if gov==1 , /// 
lc(maroon)  lpattern(dash)  /// 
||     scatter estimate time if gov==1, ///
mc(maroon) msymbol(T) /// 
ytitle("") bgcolor(white) ///
legend(order(2 "MNCs ${matched_label}" 4 "${label_name}")) ///
ytitle("Outcome: ${outc_}", size(medlarge)) ///
xtitle("Year Since Event", size(medlarge)) graphregion(color(white)) yline(0) ///
xlabel(-4 (1) 4, angle(0)) /// 
legend(region(lwidth(none)) rows(1) size(medlarge)) ///
ylabel(`ranges',labsize(large)) 
graph export "${path0}/results/\${loc_`outc'}\\${fig_n_`outc'}.eps", as(eps) replace
}
}
end

} 

********************************************************************************
*Table 4
*First-time Suppliers Start Selling to More Buying Sectors. 
*Their Sales Shift Towards Buyers with Better Performance
********************************************************************************
prog characteristics_buyers
display "Table 5: characteristics_buyers"
quiet{
set seed 123 
estimates clear
*loading data	
use $analysis_data , clear

*dummies to show in tables  	
ds D_m4-D_4
global dummies `r(varlist)'

*log number of 2-d sectors
gen l_num_sector2_sell=log(num_sector2_sell)
gen l_num_sector2_buy=log(num_sector2_buy)
gen l_num_sector4_sell=log(num_sector4_sell)
gen l_num_sector4_buy=log(num_sector4_buy)
gen lmean_trab=log(ave_b_trabaj_)
gen lmean_sales=log(ave_b_ingresosir_)
gen lave_b_nsuppliers=log(ave_b_nsuppliers)
gen lave_b_nclients=log(ave_b_nclients)
gen lave_b_length_suppliers=log(ave_b_length_suppliers)
gen lave_b_trans_per_seller=log(ave_b_trans_per_seller)

global outcomes_tab5 "l_num_sector2_sell l_num_sector2_buy l_num_sector4_sell l_num_sector4_buy lmean_trab lmean_sales lave_b_nsuppliers lave_b_nclients ave_b_sh_exp ave_b_sh_imp lave_b_trans_per_seller lave_b_length_suppliers"
local j=1
foreach v of global outcomes_tab5 {
replace `v'=. if lmean_sales==.
global REG "reghdfe `v' y D_* if past!=1 , absorb(year#sector4#province IDs) vce(cluster province#sector_g)"
if "`v'"!="lmean_sales" & "`v'"!="lave_b_trans_per_seller" {
global quant=1
}
if "`v'"=="ave_b_sh_exp" | "`v'"=="ave_b_sh_imp" {
global quant=1
global logs=0
}
run_reg 
estimates store c_`j'
local j = `j'+1
}

********
* TABLE 
********
* Prepare estimates for -estout-
estfe c_*, labels(IDs "Firm FE" year#sector4#province "Year-4DSect-Prov FE")
return list
}
esttab c_* using ${path0}/results/0-main_text/Table_4.tex , ///
 indicate(`r(indicate_fe)') ///
 star(* 0.10 ** 0.05 *** 0.01) varwidth(23) keep($dummies ) ///
 cells("b(fmt(3) star )" "se(par fmt(3))" ) ///
 stats(r2_a mean_dep sd_dep n_obs n_fe n_firms , fmt(a2) labels("Adjusted R$^2$" "Mean Dep. Var. (level)" "SD Dep. Var. (level)" "\# Observations" "\# Fixed Effects" "\# Firms" ))  ///
 mlabel("\# 2D Buying Sectors" "\# 2D Supplying Sectors" "\# 4D Buying Sectors" "\# 4D Supplying Sectors" "Number Workers" "Total Sales" "Number Suppliers" "Number Buyers" "Export Share" "Import Share" "Trans / Suppliers" "Length w/ Suppliers") ///
 alignment(c) replace noconstant label nodepvar collabels(none)  compress type
end 
 
********************************************************************************
*Table 5 and Table E9
*Model: First-Time Suppliers Have a Steep (Closer to Flat)
*Short-Run (Medium-Run) Marginal Cost Curve 
********************************************************************************
{
prog model
********************** 
***  PARAMETERS    ***  
**********************
display "estimating delta using model"
global scale_sales "y"
global salesothers_sales "sales_others"
global scale_trans "l_trans"
global salesothers_trans "trans_others"

*GOV exercise
global type_ "gov"
global label_name "Government"
	quiet sample_${type_}
	quiet suppliers 
	
*BIG DOMESTIC FIRMS exercise
global type_ "bigdom"	
global label_name "Large Domestic Buyers"
	quiet sample_${type_}
	quiet suppliers

*Domestic exporters exercise
global type_ "exp"	
global label_name "Domestic Exporters"
	quiet sample_${type_}
	quiet suppliers
	
*model results 	
	model_results

end

********************************************************************************
* model results 
********************************************************************************
prog model_results
set seed 123 
global analysis_data_mnc $analysis_data
*Model results
estimates clear 
foreach sigmas in 5.03 {
foreach case in mnc  {	
foreach set_ in sales trans {
global sigma=`sigmas'
global set_ `set_'
global case `case'
display "Event: suppliers to `case'. Sample `set_'. Sigma=`sigmas'"

*using main data
	quiet data_model
	
*getting the estimates of delta for different lags
	quiet delta_estimates

}
}
}

foreach sigmas in 5.03 {
foreach case in gov exp bigdom {	
foreach set_ in sales  {
global sigma=`sigmas'
global set_ `set_'
global case `case'
display "Event: suppliers to `case'. Sample `set_'. Sigma=`sigmas'"

*using main data
	quiet data_model
	
*getting the estimates of delta for different lags
	quiet delta_estimates

}
}
}

*
esttab mncs* mnct* using ${path0}/results/0-main_text/Table_5.tex ,  ///
 star(* 0.10 ** 0.05 *** 0.01) varwidth(15) ///
 cells("b(fmt(3) star )" "se(par fmt(3))" ) ///
 alignment(c) replace noconstant label nodepvar collabels(none)   compress	type ///
 mlabel("$\delta$" "$\gamma = \frac{\delta}{\delta + \sigma -1}$" "$\delta$" "$\gamma = \frac{\delta}{\delta + \sigma -1}$")
	
esttab mncs* govs* exps* bigdoms* using ${path0}/results/5-appendix_e\Supplementary_Table_E9.tex ,  ///
 star(* 0.10 ** 0.05 *** 0.01) varwidth(15) ///
 cells("b(fmt(3) star )" "se(par fmt(3))" ) ///
 alignment(c) replace noconstant label nodepvar collabels(none)   compress	type ///
 mlabel("mnc $\delta$" "MNC $\gamma$" "Gov. $\delta$" "Gov. $\gamma$" "Exp. $\delta$" "Exp. $\gamma$" "Large Dom. $\delta$" "Large Dom. $\gamma$")
	
end

********************************************************************************
*program: importing main data and keeping relevant variables
********************************************************************************
prog data_model
*display "data"
quiet{	
*loading data	
use ${analysis_data_${case}} , clear

*standardizing names
capture rename l_not_MNC_event sales_others
capture rename l_not_MNC_event_2 trans_others
capture rename l_not_event sales_others
capture rename l_not_event_trans trans_others

*keep relevant variables
keep ${scale_${set_}} ${salesothers_${set_}} ///
 year ID* D* event time treat* sector_g y l m k sector4 province past
compress

*in main regression 
reghdfe ${scale_${set_}}  D_* if past!=1, ///
 absorb(year#sector4#province IDs) vce(cluster province#sector_g) res(resid)
 
keep if resid!=.

*saving temp file
save temp/temp_data_model.dta, replace

}

end

********************************************************************************
*getting the estimates of delta for different lags
********************************************************************************
prog delta_estimates 


*using previous data
use temp/temp_data_model.dta , clear

bootstrap time_0=r(delta_0) time_1=r(delta_1) time_2=r(delta_2) ///
 time_3=r(delta_3) time_4=r(delta_4)  , ///
seed(123) reps(50)  : delta_bootstrap
local s=round(${sigma})
estimates store ${case}${set_}_delta_`s'

local gammas ""
forvalue j=0/4{
local gammas "`gammas' (time_`j':-_b[time_`j'] / (_b[time_`j']+$sigma - 1))"
}
nlcom  `gammas' , post 	
estimates store ${case}${set_}_gamma_`s'
end

********************************************************************************
*bootstrap
********************************************************************************
program delta_bootstrap , rclass
preserve 
global outcome_1 "${scale_${set_}}"
global controls_1 ""
global coeff_1 "y"
global outcome_2 "${salesothers_${set_}}"
global controls_2 ""
global coeff_2 "other"
global outcome_3 "y"
global controls_3 "l k m"
global coeff_3 "prod"
local sigma="$sigma"
global case_ "$case"

forvalue i=1/3{
*regressions
reghdfe ${outcome_`i'} ${controls_`i'}  D_* if past!=1, ///
 absorb(year#sector4#province IDs) vce(cluster province#sector_g)
*saving coefficients
forvalue j=0/4{
scalar ${coeff_`i'}_`j'=_b[D_`j']
if "$case_" != "mnc" & `i'==3 {
scalar ${coeff_`i'}_`j'=0
}
}
}
*delta 
forvalue j=0/4{
return scalar delta_`j'= (`=other_`j''  -( `sigma' - 1 ) * `=prod_`j'' )/ `=y_`j''
}

end
} 
 
********************************************************************************
*Table 6
*Heterogeneity in TFP Gains Based on the Sector of the Domestic Firm or the MNC
********************************************************************************
{
prog het_sector
display "HETEROGENEITY BY SECTORS"
quiet{
set seed 123 
*loading data	
use $analysis_data , clear
estimates clear
eststo clear

*** sector of suppliers

*categories 
capture drop sect 
gen sect=.
replace sect=1 if sector_g==2 
replace sect=2 if sector_g==6
replace sect=3 if sector_g!=1 & sector_g!=2 & sector_g!=6
replace sect=4 if sector_g==1 
global label_1 "Manuf"
global label_2 "Retail"
global label_3 "Services"
global label_4 "Agric"
global categ "sect"
global full "1"
global het_name "a"

*regressions 
	quiet het_regressions

*** sector of mnc
capture drop sect
gen sect=.
replace sect=1 if b_sector_g==2 
replace sect=2 if b_sector_g==6
replace sect=3 if b_sector_g!=1 & b_sector_g!=2 & b_sector_g!=6
replace sect=4 if b_sector_g==1 
global label_1 "Manuf"
global label_2 "Retail"
global label_3 "Services"
global label_4 "Agric"
global categ "sect"
global het_name "b"

*regressions 
	quiet het_regressions


 *table	
* Prepare estimates for -estout-
	estfe a_* b_* , labels(IDs "Firm FE" year#sector2#province "Year-2DSect-Prov FE")
	return list
}
esttab  a_1* a_2* b_* using ${path0}/results/0-main_text/Table_6.tex , ///
 indicate(`r(indicate_fe)' Never_Matched) star(* 0.10 ** 0.05 *** 0.01) ///
 keep(${dummies}) cells("b(fmt(2) label($\beta$) star )" "se(par fmt(2) label((SE)) )" ) ///
 stats(r2_a mean_dep sd_dep n_obs n_fe n_firms , fmt(a2) labels("Adjusted R$^2$" "Mean Dep. Var. (level)" "SD Dep. Var. (level)" "\# Observations" "\# Fixed Effects" "\# Firms" ))   ///
 mlabel("MFG" "RET" "SER" "AGR" "MFG" "RET" "SER" "AGR" "MFG" "RET" "SER" "AGR" "MFG" "RET" "SER" "AGR") alignment(c) ///
 replace noconstant label nodepvar collabels(none)  compress	type 
end 

***********************************************
*code for het regs depending on the categories 
***********************************************
prog het_regressions

global reg_ "y k l m"

*one at a time
preserve
levelsof ${categ} , local(levels)

foreach v of local levels {

*full sample 
if "$full"=="1"{
global REG "reghdfe $reg_ D_* if ${categ}==`v' & past!=1, absorb(year#sector2#province IDs) vce(robust)"
run_reg
estadd local Never_Matched "YES"
addfe Never_Matched
estimates store ${het_name}_1_`v'
} 

*only suppliers
global REG "reghdfe $reg_ D_*  if ${categ}==`v' & event!=., absorb(year#sector2#province IDs) vce(robust)"
run_reg
estadd local Never_Matched "NO"
estimates store ${het_name}_2_`v'
 
}
global full ""
restore

end
}

********************************************************************************
* Table 7 
* Heterogeneity in TFP Gains Based on the Importance of the First Transaction
********************************************************************************
prog tfp_dom_char_hete

***
***heterogeneity based on amount sold 
*** 	
quiet{
*loading data	
use $analysis_data , clear

*above/below median 
gen temp_first_trans=trans_first_mnc if time==0
bys ID: egen first_trans= mean(temp_first_trans)
global med_unit "ID"
global med_var "first_trans"
quiet ab_med

***
*** regressions 
*** 
estimates clear

foreach v in first_trans  {
forvalue i=0/1{
global REG "reghdfe y k l m  D_* if event!=. & ab_med_`v'==`i' , absorb(year#sector2#province IDs) vce(robust)"
run_reg
estimates store `v'_`i'
}
}
}
***
***heterogeneity based on "core inputs"
***
quiet{
*revec: sectors for buyers and seller 
use $revec_group , clear
keep ID firm_ciiu4
drop if firm_ciiu4=="." | firm_ciiu4=="" 
rename ID buyer 
rename firm_ciiu buyer_sector
tempfile buyers
save `buyers', replace
rename buyer seller 
rename buyer_sector seller_sector
tempfile sellers
save `sellers', replace 

*transactions
 use $transactions , clear
 merge m:1 buyer using `buyers'
 keep if _m==3
 drop _m
 merge m:1 seller using `sellers'
 keep if _m==3
 drop _m
 drop if pot==1
 drop pot
 
*trans per sector
collapse (sum) trans, by(buyer_sector seller_sector)
 *shares
bys buyer_sector: egen tot_trans=total(trans) 
gen share_in_inputs=trans/tot_trans
keep buyer_sector seller_sector share
rename seller_sector firm_ciiu4
rename buyer_sector b_firm_ciiu4
tempfile IO
save `IO', replace 
  
*merging with analysis data
use $analysis_data , clear
merge m:1 firm_ciiu4 b_firm_ciiu4 using `IO'
drop if _m==2
drop _m 

*above/below median 
egen group_sectors=group(firm_ciiu4 b_firm_ciiu4) if event!=.
global med_unit "ID"
global med_var "share_in_inputs"
quiet ab_med

*gen core input
gen core_1=(share_in_inputs>0.01) if event!=.
gen core_2=(share_in_inputs>0.05) if event!=.
gen core_3=(share_in_inputs>0.1) if event!=.

forvalue i=0/1{
global REG "reghdfe y k l m  D_* if event!=. & ab_med==`i' , absorb(year#sector2#province  IDs) vce(robust)"
run_reg
estimates store ab_core_`i'
}

forvalue i=1/3{
global REG "reghdfe y k l m  D_* if event!=. & core_`i'==1 , absorb(year#sector2#province  IDs) vce(robust)"
run_reg
estimates store core_`i'
}
}
*Table 
esttab ab_core* core* first_trans*  ///
 using ${path0}/results/0-main_text/Table_7.tex , ///
  star(* 0.10 ** 0.05 *** 0.01) keep(${dummies}) cells("b(fmt(2) label($\beta$) star )" "se(par fmt(2) label((SE)) )" ) ///
 stats(r2_a mean_dep sd_dep n_obs n_fe n_firms , fmt(a2) labels("Adjusted R$^2$" "Mean Dep. Var. (level)" "SD Dep. Var. (level)" "\# Observations" "\# Fixed Effects" "\# Firms" ))   ///
 mlabel("Below Med IO Sh" "Above Med IO Sh" "IO sh larger 1\%" "IO sh larger 5\%" "IO sh larger 10\%" "Below Med. First Trans" "Above Med. First Trans") alignment(c) ///
 replace noconstant label nodepvar collabels(none)  compress type
end

********************************************************************************
*Other
*auxiliary
*programs
******************************************************************************** 
{
********************************************************************************
*PROGRAM TO ADD FIXED EFFECTS INDICATOR TO LATEX TABLES           
********************************************************************************
program addfe
    if `"`0'"'=="" local 0 _fe
    tempname b V
    mat `b' = 0
    mat coln `b' = `0'
    mat `b' = e(b), `b'
    mat `V' = e(V)
    mat `V' = (`V', J(rowsof(`V'), 1, 0)) \ (J(1, colsof(`V'), 0), 0)
    erepost b=`b' V=`V', rename
end

********************************************************************************
* CODE FOR EVENT STUDY GRAPH
********************************************************************************
prog event_graph
local REG "$REG"
local outcome "$outcome"
local ranges "$ranges"

* BEGIN GRAPH
quiet{
local outcome: subinstr local outcome "_" " " 
local outcome: subinstr local outcome "_" " " 
local outcome: subinstr local outcome "_" " " 
local outcome: subinstr local outcome "_" " " 

preserve
parmby "`REG'",label norestore
drop if label=="" | label=="Constant"
sencode label,gene(parmlab)

*only for symmetric intervals before/after
scalar upper=_N/2
scalar lower=-`=upper'
scalar lowerplus=`=lower'+1
scalar upperminus=`=upper'-1
local time "`=lower'"
forvalue i=`=lowerplus'/`=upper'{
if `i'!=-1{
local time "`time' `i'" 
}
}
egen time = fill(`time')
replace time=`=upper' if _N==_n
drop if time==`=upper' | time==`=lower'
local new = _N + 1
set obs `new' 
local new = _N 
replace estimate=0 in `new'
replace stderr=0 in `new'
replace time=-1 in `new'

label var time "Years Since Interaction"
label var estimate "`outcome'"
local labels "`=lowerplus' `"`=lowerplus'"'"
scalar lowerplus2=`=lowerplus'+1
display `labels'
forvalue i=`=lowerplus2'/`=upperminus'{
local labels "`labels' `i' `"`i'"'"
}
display `labels'
sort time

serrbar estimate stderr time,  scale (1.96) ///
yline(0) xlabels(`labels', labsize(large)) graphregion(fcolor(white)) ytitle("Outcome: `outcome'", size(medlarge)) xtitle("Years Since First MNC Interaction", size(medlarge))  ylabel(`ranges',labsize(large)) 
restore
}
end

********************************************************************************
*run regressiona and stores relevant info
********************************************************************************
prog run_reg
capture drop resid
capture drop dep

*regression 
$REG resid(resid) 

* add number of observations 
scalar n_obs=e(N)
estadd scalar n_obs

* add distinct number of firms
distinct IDs if resid!=.
scalar n_firms=r(ndistinct)
estadd scalar n_firms 

* add mean and sd of dependent variable 
global dep_var "`e(depvar)'"
gen dep=.
if "$quant" == ""{
*in US dollars
replace dep=exp( ${dep_var} )/500
}
if "$quant" == "1" {
replace dep=exp( $dep_var )
}
if "$quant" == "1" & "$logs"=="0" {
replace dep=$dep_var
}
global quant ""
global logs ""
quietly summ dep if resid!=.
scalar mean_dep=r(mean)
scalar sd_dep=r(sd)
estadd scalar mean_dep
estadd scalar sd_dep

*add number of fixed effects 
scalar n_fe=e(df_a_initial)
estadd scalar n_fe

capture drop resid
capture drop dep
end

********************************************************************************
* PROGRAM TO GENERATE DIFF IN MEANS
********************************************************************************
program myttests, eclass
     syntax varlist [if] [in], by(varname) [ * ]
     marksample touse
     markout `touse' `by'
	 local varia_program "mu_1 mu_2 n_1 n_2 mu_1_se mu_2_se d d_se d_t d_p"
     tempname `varia_program'
      foreach var of local varlist {
         qui ttest `var' if `touse', by(`by') `options'
         mat `mu_1' = nullmat(`mu_1'), r(mu_1)
         mat `mu_2' = nullmat(`mu_2'), r(mu_2)
         mat `n_1' = nullmat(`n_1'), r(N_1)
         mat `n_2' = nullmat(`n_2'), r(N_2)		 
         mat `mu_1_se' = nullmat(`mu_1_se'), r(sd_1)
         mat `mu_2_se' = nullmat(`mu_2_se'), r(sd_2)		 
         mat `d'    = nullmat(`d'   ), r(mu_1)-r(mu_2)
         mat `d_se' = nullmat(`d_se'), r(se)
         mat `d_t'  = nullmat(`d_t' ), r(t)
         mat `d_p'  = nullmat(`d_p' ), r(p)
     }
         foreach mat in `varia_program' {
         mat coln ``mat'' = `varlist'
     }
     tempname b V
     mat `b' = `mu_1'*0
     mat `V' = `b''*`b'
     eret post `b' `V'
     eret local cmd "myttests"
     foreach mat in `varia_program' {
        eret mat `mat' = ``mat''
    }
 end 

********************************************************************************
*above/below median
********************************************************************************
prog ab_med 

egen tag=tag($med_unit )
gen temp_tag=tag*$med_var 
replace temp_tag=. if temp_tag==0
summ temp_tag, det
local med `r(p50)'
gen temp_ab_med = ($med_var >=`r(p50)') if tag!=0
replace temp_ab_med=. if $med_var ==.
bys $med_unit : egen ab_med_$med_var = max(temp_ab_med)
capture drop temp* tag  

end
 
quiet{
***  PRODUCER PRICE INDEX  (yearly average) 2013 ==100   
global ipc2005 = 50.99*100/92.88
global ipc2006 = 57.96*100/92.88
global ipc2007 = 63.38*100/92.88
global ipc2008 = 75.43*100/92.88
global ipc2009 = 77.53*100/92.88
global ipc2010 = 81.15*100/92.88
global ipc2011 = 85.57*100/92.88
global ipc2012 = 89.78*100/92.88
global ipc2013 = 92.88*100/92.88
global ipc2014 = 98.99*100/92.88
global ipc2015 = 98.93*100/92.88
global ipc2016 = 100.08*100/92.88
global ipc2017 = 100.77*100/92.88
global ipc2018 = 103.27*100/92.88
global ipc2019 = 106.25*100/92.88
global ipc2020 = 106.08*100/92.88
global ipc2021 = 107.61*100/92.88

********************************* 
***  AVERAGE EXCHANGE RATE    ***  
*********************************
* 1 March of each year: average compra-venta
global tc_2005=477.76578
global tc_2006=511.29055
global tc_2007=516.59063
global tc_2008=526.35683
global tc_2009=573.35611
global tc_2010=525.68364
global tc_2011=505.69
global tc_2012=502.8868
global tc_2013=499.75405
global tc_2014=538.36123
global tc_2015=534.55408
global tc_2016=544.76658
global tc_2017=567.55509
global tc_2018=573.80312
global tc_2019=583.82885
global tc_2020=581.54811
global tc_2021=611.75618
}
}
******************************************************************************** 
***  EXECUTE THE PROGRAM main   
********************************************************************************
main

******************************************************************************** 
***  TIMER
********************************************************************************
timer off 1
*time in seconds
timer list 1
*time in minutes
local time_ =round(`r(t1)'/60)
display "the code takes `time_' minutes in total"

log close
translate `logfile'.smcl `logfile'.txt , replace linesize(250)
erase `logfile'.smcl
